package com.zxl.live.screen.screen.ui.widget.panel;

import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.MotionEvent;

import com.zxl.live.screen.screen.utils.ScreenUtils;

import java.util.Random;

public class SnowScreenSpirit extends ScreenSpirit {

	public SnowScreenSpirit(Bitmap bitmap, Random mRandom) {
		super(bitmap, mRandom);
	}

	@Override
	public void doDraw(Camera camera, Matrix matrix, Canvas canvas, Paint paint) {
		if (isActive) {
			doDraw(canvas, paint, bitmap, scale, 255, baseX, baseY, x, y, false, rotateAngle * (isRotateAngleAdd ? 1 : -1));
		}
	}
	
	private void doDraw(Canvas canvas, Paint paint, Bitmap bitmap, float scale, int alpha, float baseX, float baseY, 
			float offsetX, float offsetY, boolean isNeedRotate, int angle) {
		if (bitmap == null || bitmap.isRecycled()) {
			return;
		}
		int saveCount = canvas.save();
		canvas.translate(baseX - bitmap.getWidth() / 2 + offsetX, baseY - bitmap.getHeight() / 2 + offsetY);
		canvas.scale(scale, scale);

		if (isNeedRotate) {
			canvas.rotate(angle, bitmap.getWidth() * 0.5f, bitmap.getHeight() * 0.5f);
		}

		if (alpha < 0) {
			paint.setAlpha(0);
		} else if (alpha > 255) {
			paint.setAlpha(255);
		} else {
			paint.setAlpha(alpha);
		}

		canvas.drawBitmap(bitmap, 0, 0, paint);
		paint.setAlpha(255);
		canvas.restoreToCount(saveCount);
	}

	@Override
	public void logic() {
		if (isActive) {
			changeX();
			changeY();
			changeRotate();
			changeRotateAngle();

			if (baseY + y - bitmap.getHeight() * 10 > ScreenUtils.sHeightPixels
					|| baseX + x - bitmap.getWidth() *1.5 > ScreenUtils.sWidthPixels) {
				reset();
				return;
			}
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return false;
	}

}
